package com.mszl.blog.common.task;

import com.mszl.blog.dao.mapper.ArticleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.Set;

@Component
public class IncrTask {
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private ArticleMapper articleMapper;
    static String key="readNumber";
    static String key2="commentNumber";
    @Scheduled(fixedRate = 60000)
    public void incrViewNumber(){

        Set<String> ids = redisTemplate.opsForZSet().range(key, 0, -1);
        if (ids.isEmpty()) return;
        for (String id : ids) {
            articleMapper.updateView(Long.valueOf(id),redisTemplate.opsForZSet().score(key,id));
        }
    }
    @Scheduled(fixedRate = 60000)
    public void incrCommentNumber(){
        Set<String> ids = redisTemplate.opsForZSet().range(key2, 0, -1);
        if (ids.isEmpty()) return;
        for (String id : ids) {
            articleMapper.updateComment(Long.valueOf(id),redisTemplate.opsForZSet().score(key2,id));
        }
    }

}
